<?php

namespace App\Admin\Controllers;

use function foo\func;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class RoleController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('修改角色信息');
            $content->description('...');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('新建角色');
            $content->description('...');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Role::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                // 在这里添加字段过滤器
                $filter->like('name', 'name');
            });
            $grid->name('角色')->badge();
            $grid->guard_name('看守器');
            $grid->description('描述');
            $grid->permissions('权限')->display(function($permissions){
                return collect($permissions)->map(function ($permission,$key){
                    if($key==4){
                        return "<span class='label label-success'>".$permission['name']."</span>  ...";
                    }else{
                        return "<span class='label label-success'>".$permission['name']."</span>";
                    }
                });
            })->take(5)->implode('  ')/*->pluck('name')->label()*/;
            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Role::class, function (Form $form) {
            $form->hidden('id', 'ID');
            $form->text('name', 'Name')->rules(function ($form){
                // 如果不是编辑状态，则添加字段唯一验证
                if (!$id = $form->model()->id){
                    return 'unique:roles';
                }else{
                    return 'unique:roles,name,'.$id;
                }
            });
            $form->listbox('permissions', trans('admin.permissions'))->options(Permission::all()->pluck('name', 'id'));
            /*$form->saving(function($form){
                if($form->name !== $form->model()->name && Role::where('name',$form->name)->value('id')){
                    $error = new MessageBag(['title'=>'提示','message'=>'邮箱已存在!']);
                    return back()->withInput()->with(compact('error'));
                }
            });*/
            $form->text('description', 'Description')->rules('max:100');
        });
    }
}
